
import pytest
import allure
from allure_commons.types import AttachmentType

def attach_screenshot_on_failure(browser, test_name):
    """失败时自动截图"""
    screenshot_path = f"reports/screenshots/{test_name}.png"
    browser.save_screenshot(screenshot_path)
    allure.attach(
        browser.get_screenshot_as_png(),
        name=f"{test_name}_failure",
        attachment_type=AttachmentType.PNG
    )

# 在测试用例中使用
def test_with_screenshot(browser):
    try:
        # 测试逻辑
        assert True
    except AssertionError:
        attach_screenshot_on_failure(browser, "test_with_screenshot")
        raise